import { login, logout, getInfo, judgeLogin, queryForList, getByIdMoreDetail, createOrUpdate, deleteData } from '@/api/system/systemUserApi'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter, setRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: ''
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {

  getByIdMoreDetail({ commit }, dataId) {
    return new Promise((resolve, reject) => {
      getByIdMoreDetail(dataId).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  createOrUpdate({ commit }, params) {
    return new Promise((resolve, reject) => {
      createOrUpdate(params).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  login({ commit }, userInfo) {
    const { account, password, publicKey, captcha, accountType } = userInfo
    return new Promise((resolve, reject) => {
      login({ account: account.trim(), password: password, publicKey: publicKey, captcha: captcha.trim(), accountType: accountType }).then(response => {
        const { data } = response
        const { systemUser } = data
        const { menuList } = data
        sessionStorage.setItem('user', JSON.stringify(systemUser))
        commit('SET_TOKEN', systemUser.id)
        setToken(systemUser.id)
        // 缓存用户信息
        commit('SET_NAME', systemUser.userName)
        resolve(systemUser)
      }).catch(error => {
        reject(error)
      })
    })
  },

  queryForList({ commit }, params) {
    return new Promise((resolve, reject) => {
      queryForList(params).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },


  judgeLogin({ commit }) {
    return new Promise((resolve, reject) => {
      judgeLogin().then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }
        // 缓存用户信息
        const { userName } = response.data
        sessionStorage.setItem('user', JSON.stringify(data))
        commit('SET_NAME', userName)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },


  deleteData({ commit }, dataId) {
    return new Promise((resolve, reject) => {
      deleteData(dataId).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  }


}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

